package com.tl.sun.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.R;
import com.tl.sun.model.BaseModel;
import com.tl.sun.ui.adapter.CityAdapter;
import com.tl.sun.utils.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/14.
 */

public class CityActivity extends BaseActivity {

    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.rv_ctiy)
    RecyclerView mRvCtiy;

    private CityAdapter mCityAdapter;
    private List<String> enetiyList = new ArrayList<>();
    private String province;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_city);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            province = bundle.getString("province");
          //  getCity(province);
        }
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }
    public void init() {
        mTvGenericTitle.setText("线路选择");
    }

    @OnClick(R.id.rl_generic_back)
    public void onClick() {
        onBackPressed();
    }


    public void getCity(final String province) {
        HashMap<String, String> map = new HashMap<>();
        map.put("province", province);
        map.put("platform", "android");
        JSONObject jsonObject = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl()+"server/get_city")
                .tag(this)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel<List<String>> baseModel = gson.fromJson(s, new TypeToken<BaseModel<List<String>>>() {
                        }.getType());
                        if (baseModel.getRet().equals("0")) {
                            enetiyList = baseModel.getData();
                         //   mCityAdapter = new CityAdapter(enetiyList);
                            mRvCtiy.setLayoutManager(new LinearLayoutManager(CityActivity.this));
                            mRvCtiy.setAdapter(mCityAdapter);
                            mRvCtiy.addOnItemTouchListener(new OnItemClickListener() {


                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    super.onItemClick(adapter, view, position);
                                    Intent city = new Intent(CityActivity.this, LineActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putString("province", province);
                                    bundle.putString("city", enetiyList.get(position));
                                    city.putExtras(bundle);
                                    startActivity(city);
                                    finish();
                                }

                                @Override
                                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                                }
                            });
                        } else {
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });
    }
}
